package com.baizhi.controller;

import com.baizhi.entity.Drug;
import com.baizhi.service.DrugService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("drug")
public class DrugController {
    @Autowired
    private DrugService drugService;

    @RequestMapping("show")
    public Map showDrug(String name,Integer page,Integer limit){
        Map map = new HashMap();
        PageInfo<Drug> pageInfo = drugService.selectDrugs(name, page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }
    @RequestMapping("add")
    public Map addDrug(Drug drug){
        Map map = new HashMap();
        try {
            drugService.insertDrug(drug);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    public Map removeDrug(int drugid){
        Map map = new HashMap();
        try {
            drugService.deleteDrug(drugid);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    public Drug getOneDrug(int drugid){
        return drugService.selectById(drugid);
    }
    @RequestMapping("update")
    public Map updateDrug(Drug drug){
        Map map = new HashMap();
        try {
            drugService.updateDrug(drug);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
}
